package com.siyu.tickets.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siyu.common.page.Pagination;
import com.siyu.common.page.SimplePage;
import com.siyu.common.web.CookieUtils;
import com.siyu.core.web.WebErrors;
import com.siyu.tickets.entity.Order;
import com.siyu.tickets.manager.OrderMng;

/**
 * 订单相关
 * 
 * @since 2013-7-15
 * @version 0.1
 * @author cuigx
 */
@Controller
public class OrderAct {

	private static final Logger log = LoggerFactory.getLogger(OrderAct.class);

	@RequestMapping("/order/list")
	public String list(String name, Date beginTime, Date endTime, Boolean valid, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(name, beginTime, endTime, valid, SimplePage.cpn(pageNo),
				CookieUtils.getPageSize(request));
		model.addAttribute("name", name);
		model.addAttribute("valid", valid);
		model.addAttribute("beginTime", beginTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		return "order/list";
	}

	@RequestMapping("/order/add")
	public String newOrder(Order order, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		WebErrors errors = validateSubmit(order, request, response);
		if (!errors.hasErrors()) {
			try {
				manager.newOrder(order);
				log.info("new order :{1}", order.getId());
				model.addAttribute("message", "下单成功！");
				model.addAttribute("order", order);
			} catch (Exception e) {
				e.printStackTrace();
				errors.addError(e.getMessage());
			}
		}
		if (errors.hasErrors()) {
			errors.toModel(model);
		}
		return "order/detail";
	}

	@RequestMapping("/order/{id}")
	public String detail(@PathVariable(value = "id") Integer id, HttpServletRequest request, ModelMap model) {
		Order order = manager.findById(id);
		model.addAttribute("order", order);
		return "order/detail";
	}

	private WebErrors validateSubmit(Order order, HttpServletRequest request, HttpServletResponse response) {
		WebErrors errors = WebErrors.create(request);
		if (errors.ifNull(order.getQuantity(), "quantity")) {
			return errors;
		}
		if (errors.ifBlank(order.getConsumerName(), "consumerName", 100)) {
			return errors;
		}
		if (errors.ifBlank(order.getConsumerIdcard(), "consumerIdcard", 100)) {
			return errors;
		}
		if (errors.ifBlank(order.getConsumerMobile(), "consumerMobile", 100)) {
			return errors;
		}
		if (errors.ifBlank(order.getSalesman(), "salesman", 100)) {
			return errors;
		}
		return errors;
	}

	@Autowired
	private OrderMng manager;

}
